if(files.cwd().endsWith("脚本")){
  let _require = require;
  require = function(path){
    if(path.startsWith("./")){
      path = path.replace("./","");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}


auto.waitFor()
var utils = require("./utils.js");
var storage = storages.create("xiangxiangyuedu"); 
var time = random(2000, 3000)

function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
    toastLog("检查是否有打开权限!")
    while (true) {
      var allow = text("允许").findOne(5000)
      if (allow) {
        sleep(1000)
        toastLog("发现允许授权!")
        allow.click()
        sleep(1000)
        thread.interrupt()
        break
      }
    }
  })
  sleep(1000)
  var result = launchApp(app_name)
  sleep(3000)
  thread.interrupt()
  return result
}
 
function task() {
    var notice = id("notice_delete").findOne(2000);
    if(notice){
      notice.click();
    }else{
      log('没有通知');
    }
    sigeIn();
    var 今日任务 = id("channel_img").findOne(20000);
    if(今日任务){
        click(580,980);
        sleep(2000)
        for(var i=0;i<2;i++){
          sleep(1000)
          var finalActivity = currentActivity();
          click(random(590,610),random(650,660));
          sleep(5000);
          utils.waitAdvert(finalActivity);
          sleep(3000)
      }
      // var qfx = id("tv_QFX").findOne(1000);
      // if(qfx){
      //   qfx.click();
      // }
      back();
    }
    var 领取 = text('领取').findOne(20000)
    if (领取) {
      id("img_LQ").findOne().click();
      sleep(5000)
      log("领取完成.")
    }else{
        log("已领取")
    }
}

function sigeIn(){    
  var 每日签到 = id("channel_img").findOne(20000);
  if(每日签到){
    var finalActivity = currentActivity();
    click(238,930);
    sleep(5000);
    utils.waitAdvert(finalActivity);
    sleep(3000)
  }
  else{
    toastLog('未找到签到');
    log('未找到签到')
  }
}



function main() {
  let open_app = launchAppEnhance("UU共享");
  if (open_app) {
    task()
  }
  else {
    toastLog("UU共享打开失败，请确认是否安装并授权打开!");
  }
}

try {
  main();
  utils.updateAppProgress(100);
} catch (error) {
  toastLog(error);
}

storage.put("script_running_status", "end");
